package cn.springstudio.util;

import android.os.Bundle;
import android.util.SparseArray;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import cn.springstudio.springui.R;

/**
 * 名称：<b>FragmentFactory</b>
 * <p>作者：SpringStudio</p>
 * <p>版本：1.0</p>
 * <p>日期：2021/08/15 8:53</p>
 * <p>说明：</p>
 */
public class FragmentNavigation {

    // 当前显示的fragment
    private static final String CURRENT_FRAGMENT = "STATE_FRAGMENT_SHOW";

    @IdRes
    int mContainer;
    private final SparseArray<Fragment> mFragments = new SparseArray<>();
    private final OnFragmentNavigationListener mListener;
    private final FragmentManager mManager;
    private Fragment mCurrentFragment;

    public FragmentNavigation(FragmentManager manager, @IdRes int container, OnFragmentNavigationListener listener) {
        mListener = listener;
        mManager = manager;
        mContainer = container;
    }

    public void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            // 内存重启时调用
            int position = savedInstanceState.getInt(CURRENT_FRAGMENT,0);
            navigationTo(position);
        } else {
            // 正常启动时调用
            navigationTo(0);
        }
    }

    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(CURRENT_FRAGMENT, mFragments.indexOfValue(mCurrentFragment));
    }

    public static FragmentNavigation instance(FragmentManager manager, @IdRes int container,
                                              OnFragmentNavigationListener listener) {
        return new FragmentNavigation(manager, container, listener);
    }

    public void navigationTo(int position) {
        Fragment fragment = mFragments.get(position);
        if (fragment == null) {
            fragment = mListener.onCreateFragment(position);
            mFragments.put(position, fragment);
        }
        if (fragment != null && fragment == mCurrentFragment) {
            return;
        }
        FragmentTransaction ft = mManager.beginTransaction();
        int prev = mFragments.indexOfValue(mCurrentFragment);
        if (prev < position) {
            ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out,
                    R.anim.slide_left_in, R.anim.slide_right_out);
        } else {
            ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_right_out,
                    R.anim.slide_right_in, R.anim.slide_left_out);
        }
        if (mCurrentFragment != null && mCurrentFragment.isAdded()) {
            ft.hide(mCurrentFragment);
        }
        if (fragment != null) {
            if (fragment.isAdded()) {
                ft.show(fragment);
            } else {
                ft.add(mContainer, fragment, fragment.getClass().getName());
            }
        }
        ft.commit();
        mCurrentFragment = fragment;
    }

    public Fragment getCurrentFragment() {
        return mCurrentFragment;
    }

    public <T extends Fragment> T getFragment(Class<T> fragmentClass) {
        return (T) mManager.findFragmentByTag(fragmentClass.getName());
    }

    public interface OnFragmentNavigationListener {
        Fragment onCreateFragment(int position);
    }
}
